import numpy as np
import math
import pickle
f = open('allX.pickle', 'rb')
allX = pickle.load(f)
f = open('allY.pickle', 'rb')
allY = pickle.load(f)

def calcSimilarity(S1, S2): # 越小越相似
    ret = 0
    for i in range(len(S1)):
        ret += math.fabs(S1[i]-S2[i])
    return ret

def findMostSimilar(S1, group):
    mostSim = None
    mostSimSeq = None
    for S2 in group:
        sim = calcSimilarity(S1, S2)
        if mostSim is None or sim<mostSim:
            mostSim = sim
            mostSimSeq = S2
    return mostSimSeq

def pointDiff(s1, s2, I=0.01):
    sub = math.fabs(s1-s2)
    print(sub)
    return sub-I

def seqDiff(S1, S2):
    subSeq = []
    for i in range(len(S1)):
        subSeq.append(pointDiff(S1[i], S2[i]))
    return max(subSeq)

# 把样本按Y分类
positive = []
negative = []
for i in range(len(allY)):
    if allY[i] == 1:
        positive.append(allX[i])
    else:
        negative.append(allX[i])
# 分析所有正例
sampleRet = []
for S1 in negative:
    S2 = findMostSimilar(S1, positive)
    seqDiffVal = seqDiff(S1, S2)
    sampleRet.append(seqDiffVal)

f = open('sample.pickle', 'wb')
pickle.dump(sampleRet, f)